<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import Captcha from '../components/Captcha.vue'

const router = useRouter()
const captchaRef = ref(null)

const formData = reactive({
  username: '',
  password: '',
  captcha: ''
})

const loading = ref(false)
const errorMessage = ref('')

const handleLogin = async () => {
  try {
    loading.value = true
    errorMessage.value = ''
    
    const loginData = {
      username: formData.username,
      password: formData.password
    }
    
    console.log('发送登录请求:', loginData)
    
    const response = await fetch('http://localhost:3001/api/admin/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
      body: JSON.stringify(loginData)
    })
    
    console.log('响应状态:', response.status)
    const data = await response.json()
    console.log('响应数据:', data)
    
    if (response.ok && data.code === 200) {
      // 保存 token 和用户信息
      localStorage.setItem('token', data.data.token)
      localStorage.setItem('userInfo', JSON.stringify(data.data.userInfo))
      
      // 跳转到首页
      router.push('/')
    } else {
      errorMessage.value = data.message || '登录失败'
      formData.password = ''
    }
  } catch (error) {
    console.error('登录请求失败:', error)
    errorMessage.value = '网络错误，请稍后重试'
    formData.password = ''
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-box">
        <div class="login-header">
          <h2>社区团购管理系统</h2>
          <p class="subtitle">欢迎回来，请登录您的账号</p>
        </div>
        
        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-item">
            <div class="input-wrapper">
              <i class="fa-solid fa-user input-icon"></i>
              <input
                v-model="formData.username"
                type="text"
                placeholder="请输入用户名"
                required
              >
            </div>
          </div>
          
          <div class="form-item">
            <div class="input-wrapper">
              <i class="fa-solid fa-lock input-icon"></i>
              <input
                v-model="formData.password"
                type="password"
                placeholder="请输入密码"
                required
              >
            </div>
          </div>

          <div class="form-item">
            <div class="captcha-wrapper">
              <div class="input-wrapper">
                <i class="fa-solid fa-shield-halved input-icon"></i>
                <input
                  v-model="formData.captcha"
                  type="text"
                  placeholder="请输入验证码"
                  required
                  maxlength="4"
                >
              </div>
              <Captcha ref="captchaRef" />
            </div>
          </div>
          
          <div v-if="errorMessage" class="error-message">
            <i class="fa-solid fa-circle-exclamation"></i>
            {{ errorMessage }}
          </div>
          
          <button 
            type="submit"
            :disabled="loading"
            class="login-button"
          >
            <i class="fa-solid fa-right-to-bracket"></i>
            {{ loading ? '登录中...' : '登录' }}
          </button>
        </form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  padding: 20px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: auto;
}

.login-container {
  width: 100%;
  max-width: 440px;
  perspective: 1000px;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  transform-style: preserve-3d;
  animation: loginAppear 0.6s ease-out;
}

@keyframes loginAppear {
  from {
    opacity: 0;
    transform: translateY(20px) rotateX(-10deg);
  }
  to {
    opacity: 1;
    transform: translateY(0) rotateX(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 24px;
}

h2 {
  font-size: 28px;
  color: #1890ff;
  margin-bottom: 12px;
  font-weight: 600;
}

.subtitle {
  color: #666;
  font-size: 14px;
}

.login-form {
  margin-top: 24px;
}

.form-item {
  margin-bottom: 20px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: #f5f5f5;
  border-radius: 8px;
  transition: all 0.3s;
}

.input-wrapper:focus-within {
  background: white;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.input-icon {
  color: #999;
  font-size: 16px;
  width: 40px;
  text-align: center;
}

input {
  flex: 1;
  padding: 12px;
  border: none;
  background: transparent;
  font-size: 14px;
  color: #333;
}

input:focus {
  outline: none;
}

input::placeholder {
  color: #999;
}

.captcha-wrapper {
  display: flex;
  gap: 12px;
  align-items: center;
}

.captcha-wrapper .input-wrapper {
  flex: 1;
}

.error-message {
  background: #fff2f0;
  border: 1px solid #ffccc7;
  padding: 8px 12px;
  border-radius: 8px;
  color: #ff4d4f;
  font-size: 14px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.login-button {
  width: 100%;
  padding: 12px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.login-button:hover {
  background: #40a9ff;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.login-button:disabled {
  background: #d9d9d9;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

@media (max-width: 480px) {
  .login-box {
    padding: 24px;
  }
  
  h2 {
    font-size: 24px;
  }
}
</style> 